package main

import (
	"fmt"
	"go_self_excise/model"
	"math/rand"
	"sort"
)

func main() {
	/*
		封装
	*/
	account := model.NewAccount("张二狗", "123456", 2000.0)
	account.Despite(500, "123456")
	account.WithDraw(600, "123456")
	account.Query("123456")

	/*
		继承
	*/
	pupil := &Pupil{}
	pupil.Student.Name = "tom"
	pupil.Student.Age = 10
	pupil.int = 20

	pupil.SetScore(100)
	pupil.ShowInfo()

	graduate := &Graduate{}
	graduate.Name = "mary"
	graduate.Age = 23
	graduate.Score = 90
	graduate.ShowInfo()
	graduate.rested()

	/*
		接口，题一：
	*/
	var herosSlice HeroSlice
	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("英雄%d", rand.Intn(100)),
			Age:  rand.Intn(100),
		}
		herosSlice = append(herosSlice, hero)
	}
	//排序前
	for _, v := range herosSlice {
		fmt.Println(v)
	}
	sort.Sort(herosSlice)
	fmt.Print("-----------------------------")
	//排序后
	for _, v := range herosSlice {
		fmt.Println(v)
	}

	/*
		接口，题二：
	*/
	phone := Phone{"华为"}
	camera := Camera{"尼康"}
	computer := Computer{}

	computer.Working(phone)
	computer.Working(camera)

	var usbArr [3]Usb
	usbArr[0] = Phone{"华为"}
	usbArr[1] = Phone{"xiaomi"}
	usbArr[2] = Camera{"尼康"}
	fmt.Println(usbArr)

	/*
		断言：
		写一函数，循环判断传入参数的类型
	*/
	newMap := make(map[int]string)
	TypeJudge(1, 3.2, 5.6664, "nihao", false, [3]int{0, 0, 0}, [3][4]string{}, newMap)

}

// Student 学生结构体
type Student struct {
	Name string
	int
	Age   int
	Score int
}

func (stu *Student) ShowInfo() {
	fmt.Printf("学生姓名%s,年龄%d，体重%d，分数%d\n", stu.Name, stu.Age, stu.int, stu.Score)
}

func (stu *Student) SetScore(score int) {
	stu.Score = score
}

// Pupil 小学生结构体
type Pupil struct {
	Student
}

type Graduate struct {
	Student
}

func (graduate *Graduate) rested() {
	fmt.Println("大学生已经放假。。。。。")
}

type Hero struct {
	Name string
	Age  int
}
type HeroSlice []Hero

// Len 实现sort方法的len接口
func (heros HeroSlice) Len() int {
	return len(heros)
}

// Less 实现sort方法的Less接口
func (heros HeroSlice) Less(i, j int) bool {
	return heros[i].Age < heros[j].Age
}

// Swap 实现sort方法的Swap接口
func (heros HeroSlice) Swap(i, j int) {
	heros[i], heros[j] = heros[j], heros[i]
}

type Usb interface {
	Start()
	Stop()
}

type Phone struct {
	Name string
}

func (p Phone) Start() {
	fmt.Printf("%v手机开始工作。。。。。\n", p.Name)
}

func (p Phone) Stop() {
	fmt.Printf("%v手机停止工作。。。。。\n", p.Name)
}

type Camera struct {
	Name string
}

func (c Camera) Start() {
	fmt.Println("相机开始工作。。。。。")
}

func (c Camera) Stop() {
	fmt.Println("相机停止工作。。。。。。")
}

type Computer struct{}

func (c1 Computer) Working(usb Usb) {
	usb.Start()
	usb.Stop()
}

// TypeJudge 接口：类型判断
func TypeJudge(items ...interface{}) {
	for index, item := range items {
		switch item.(type) {
		case string:
			fmt.Printf("第%d个参数是string类型，%s\n", index, item)
		case bool:
			fmt.Printf("第%d个参数是bool类型，%s\n", index, item)
		case float32, float64:
			fmt.Printf("第%d个参数是浮点类型，%s\n", index, item)
		case int, int32, int64:
			fmt.Printf("第%d个参数是整数类型，%s\n", index, item)
		default:
			fmt.Printf("第%d个参数是类型不确定，%s\n", index, item)
		}
	}
}
